window.onload = cf_init;

function cf_init() {

}

function warning(text) {
    swal({
        title: '提示',
        text: text,
        type: "warning",
    });
}

function success(text, callback) {
    swal({title: 'success', text: text, type: "success"}, function (e) {
        isset(callback) && callback();
    });
}

function error(text) {
    swal({
        title: '提示',
        text: text,
        type: "error",
    });
}

function isset(e) {
    // 判断对象是否存在
    return typeof(e) != "undefined" ? true : false;
}

function toUrl(url) {
    window.location.href = url;
}
function reload() {
    // 刷新当前页面
    window.location.reload();
}
function reloadSource() {
    // 刷新来源页面
    self.opener.location.reload();
}
function post(option) {
    // post提交数据
    $.ajax({
        url: option.url,
        type: "post",
        dataType: "json",
        data: option.data,
        async: false,
        success: function (result) {
            if (isset(option.success) === true) option.success(result);
        },
        error: function (error) {
            if (isset(option.error) === true) option.error(error);
        },
        beforeSend: function (xhr) {
            if (isset(option.before) === true) {
                option.before(xhr);
            } else {
                $("body").append('<div class="sk-spinner sk-spinner-fading-circle" style="z-index:99999;position:absolute;top:60px;left:0;right:0;"><div class="sk-circle1 sk-circle"></div><div class="sk-circle2 sk-circle"></div><div class="sk-circle3 sk-circle"></div><div class="sk-circle4 sk-circle"></div><div class="sk-circle5 sk-circle"></div><div class="sk-circle6 sk-circle"></div><div class="sk-circle7 sk-circle"></div><div class="sk-circle8 sk-circle"></div><div class="sk-circle9 sk-circle"></div><div class="sk-circle10 sk-circle"></div><div class="sk-circle11 sk-circle"></div><div class="sk-circle12 sk-circle"></div></div>');
            }
        },
        complete: function () {
            if (isset(option.ok) === true) {
                option.ok();
            } else {
                $(".sk-spinner").remove();
            }
        }
    });
}

function get(option) {
    // get提交数据
    $.ajax({
        url: option.url,
        type: "get",
        dataType: "json",
        data: option.data,
        async: false,
        success: function (result) {
            if (isset(option.success) === true) option.success(result);
        },
        error: function (error) {
            if (isset(option.error) === true) option.error(error);
        },
        beforeSend: function (xhr) {
            if (isset(option.before) === true) {
                option.before(xhr);
            } else {
                $("body").append('<div class="sk-spinner sk-spinner-fading-circle" style="z-index:99999;position:absolute;top:60px;left:0;right:0;"><div class="sk-circle1 sk-circle"></div><div class="sk-circle2 sk-circle"></div><div class="sk-circle3 sk-circle"></div><div class="sk-circle4 sk-circle"></div><div class="sk-circle5 sk-circle"></div><div class="sk-circle6 sk-circle"></div><div class="sk-circle7 sk-circle"></div><div class="sk-circle8 sk-circle"></div><div class="sk-circle9 sk-circle"></div><div class="sk-circle10 sk-circle"></div><div class="sk-circle11 sk-circle"></div><div class="sk-circle12 sk-circle"></div></div>');
            }
        },
        complete: function () {
            if (isset(option.ok) === true) {
                option.ok();
            } else {
                $(".sk-spinner").remove();
            }
        }
    });
}

function datatable(obj, url, columns, option) {
    if (isset(option) === false) option = {};
    if (isset(option.searching) === false) option.searching = true;
    if (isset(option.placeholder) === false) option.placeholder = "";
    if (isset(option.bLengthChange) === false) option.bLengthChange = true;
    if (isset(option.iDisplayLength) === false) option.iDisplayLength = 10;
    if (isset(option.bStateSave) === false) option.bStateSave = true;
    return $(obj).DataTable({
        "bSort": false,
        "bAutoWidth": false,
        "iDisplayLength": option.iDisplayLength,
        "aLengthMenu": [10, 30, 100],
        "bLengthChange": option.bLengthChange,
        "bStateSave": option.bStateSave,
        "bProcessing": true,
        "oLanguage": {
            "sProcessing": "正在加载...",
            "oPaginate": {"sFirst": "首页", "sPrevious": "上一页", "sNext": "下一页", "sLast": "尾页"},
            "sZeroRecords": "无数据",
            "sInfo": "显示 _START_ - _END_ 共 _TOTAL_ 条数据",
            "sLengthMenu": "显示 _MENU_ 条数据",
            "placeholder": option.placeholder,
        },
        "bServerSide": true,
        "sAjaxSource": url,
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "columns": columns,
        "searching": option.searching
    });
}

function openWindow(url, width, height) {
    var left = (window.screen.width - width) / 2;
    var top = (window.screen.height - height) / 2;
    window.open(url, '', 'height=' + height + ', width=' + width + ', top=' + top + ', left=' + left + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
}

function news(options) {
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "progressBar": true,
        "positionClass": "toast-top-center",
        "onclick": null,
        "showDuration": "500",
        "hideDuration": "1000",
        "timeOut": "1500",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }
    switch (options.type) {
        case 'success':
            toastr.success(options.title, options.content);
            break;
        case 'error':
            toastr.error(options.title, options.content);
            break;
    }
}

function swal_confirm(title, content, callback) {
    swal({
        title: title,
        text: content,
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: '是的',
        cancelButtonText: '让我再考虑一下...',
        closeOnConfirm: false
    }, function (isConfirm) {
        if (isConfirm) {
            callback();
        }
    });
}

function swal_del_msg(title, text, url) {
    swal({
        title: title,
        text: text,
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除！",
        cancelButtonText: "让我再考虑一下…",
        closeOnConfirm: false
    }, function (isConfirm) {
        if (isConfirm) {
            get({
                url: url,
                success: function (res) {
                    swal({title: "删除成功！", text: "您已经永久删除了这条信息。", type: "success"}, function () {
                        reload();
                    });
                },
                error: function (error) {
                    swal("错误", JSON.parse(error.responseText).info, "error");
                }
            })
        }
    });
}

$(function () {
    $(".select2").select2();
    $(".select2.no-search").select2({
        minimumResultsForSearch: -1
    });
});